/**
Copyright 2011 Kathryn Neugent

This file is part of Joggle.

Joggle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Joggle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Joggle.  If not, see <http://www.gnu.org/licenses/>.
*/
package edu.gwu.cs.joggle;


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.security.SecureRandom;
import java.util.ArrayList;

/**
 * @author Kathryn Neugent <kathrynneugent@gmail.com>
 * @version 1
 * @since 2011-12-20
 */

public class GUI implements ActionListener {

    protected Server server;
    protected JFrame loginFrame;
    protected JFrame welcomeFrame;
    protected JFrame auditFrame;
    protected JFrame adminFrame;
    protected JFrame newUserFrame;
    protected JFrame newGameFrame;
    protected char[][] vals;
    protected int size;
    protected JTextField userName;
    protected JPasswordField passwd;
    protected JTextField nfName;
    protected JTextField nlName;
    protected JTextField nuName;
    protected JTextField password;
    protected JTextField email;
    protected JTextField words;
    protected JComboBox openGames;
    protected JComboBox numPlayers;
    protected JComboBox boardSize;
    protected JComboBox blockUser;
    protected JComboBox resetUser;
    protected JComboBox auditReq;
    protected JCheckBox auditBox;
    protected JCheckBox adminBox;
    protected ButtonGroup group;
    protected Player player;
    protected Administrator administrator;
    protected Auditor auditor;
    protected Integer pointVal;

    // new constructor that requires the Client this GUI will be attacked to
    /**
     * Creates a GUI with the specified board, size and attached to the server
     * @param boardVals characters on the board
     * @param s size of the board
     * @param srv Server
     */
    public GUI(char[][] boardVals, int s, Server srv) {
	vals = boardVals;
	size = s;
	server = srv;

	javax.swing.SwingUtilities.invokeLater(new Runnable() {
		public void run() {
		    createAndShowGUI();
		}
	    });
    }
    
    /**
     * Creates a GUI attached to the specified server
     * @param srv Server
     */
    public GUI(Server srv) {
    	this(new char[4][4], 4, srv);
    	SecureRandom boardGenerator = new SecureRandom();
    	
		// put letters on the board
		for(int x = 0; x < size; x++) {
			for(int y = 0; y < size; y++) {
				vals[x][y] = Character.toUpperCase((char) (boardGenerator.nextInt(26) + 97));
			}
		}
    }

    public void createAndShowGUI() {
	// Set up the login frame
	loginFrame = new JFrame("Joggle Login");
	loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	loginFrame.setSize(300,350);

	// Set up login pane
	addComponentsToLogin(loginFrame.getContentPane());

	// Display the login frame
	loginFrame.setVisible(true);

	// set up welcome frame
	/*
	welcomeFrame = new JFrame("Joggle Welcome!");
	welcomeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	welcomeFrame.setSize(300,350);
	addComponentsToWelcome(welcomeFrame.getContentPane());
	*/

	// newUser
	newUserFrame = new JFrame("Create An Account");
	newUserFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	newUserFrame.setSize(300,350);
	addComponentsToNewUser(newUserFrame.getContentPane());

	// new game
	newGameFrame = new JFrame("New Game");
	newGameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	newGameFrame.setSize(300,350);
	addComponentsToNewGame(newGameFrame.getContentPane());

	// auditor frame
	auditFrame = new JFrame("Auditor");
	auditFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	auditFrame.setSize(300,350);
	addComponentsToAudit(auditFrame.getContentPane());

	// admin frame
	adminFrame = new JFrame("Administrator");
	adminFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	adminFrame.setSize(300,350);
	addComponentsToAdmin(adminFrame.getContentPane());

    }

    /**
     * Adds the components to the login pane
     * @param pane Container pane
     */
    public void addComponentsToLogin(Container pane) {
	// set layout
	pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
	JPanel panePanel = new JPanel();
	panePanel.setSize(300,350);

	// add banner
	JLabel banner = new JLabel(new ImageIcon(getClass().getResource("/assets/images/JoggleBanner.png")));
	panePanel.add(banner,BorderLayout.CENTER);

	// text
	JLabel acc1 = new JLabel("The best, most secure online Boggle(TM)");
	JLabel acc2 = new JLabel("clone ever to be produced at GWU!");
	panePanel.add(acc1,BorderLayout.CENTER);
	panePanel.add(acc2,BorderLayout.CENTER);

	// create new user
	JButton newUser = new JButton("Create an Account");
	newUser.addActionListener(this);
	panePanel.add(newUser,BorderLayout.CENTER);

	// text
	JLabel signIn = new JLabel("Or, sign in with your username and password:");
	panePanel.add(signIn,BorderLayout.CENTER);
	
	JPanel userPass = new JPanel();
	userPass.setLayout(new FlowLayout());

	// username
	JLabel uName = new JLabel("Username");
	userPass.add(uName);    
	userName = new JTextField(15);
	userName.setMaximumSize(userName.getPreferredSize());
	userName.addActionListener(this);
	userPass.add(userName);

	// password
	JLabel pwd = new JLabel("Password");
	userPass.add(pwd);
	passwd = new JPasswordField(15);
	passwd.setMaximumSize(passwd.getPreferredSize());
	passwd.addActionListener(this);
	userPass.add(passwd);
	userPass.setPreferredSize(new Dimension(300,75));

	panePanel.add(userPass,BorderLayout.CENTER);

	auditBox = new JCheckBox("Enter as Auditor");
	auditBox.addActionListener(this);
	panePanel.add(auditBox,BorderLayout.CENTER);

	adminBox = new JCheckBox("Enter as Admin");
	adminBox.addActionListener(this);
	panePanel.add(adminBox,BorderLayout.CENTER);

	// submit
	JButton submit = new JButton("Log-in");
	submit.addActionListener(this);
	panePanel.add(submit,BorderLayout.CENTER);

	pane.add(panePanel);
    }

    /**
     * adds components to the welcome pane
     * @param pane Container pane
     */
    public void addComponentsToWelcome(Container pane) {
	pane.setLayout(new BoxLayout(pane,BoxLayout.Y_AXIS));
	JLabel banner = new JLabel(new ImageIcon(getClass().getResource("/assets/images/JoggleBanner.png")));
	banner.setAlignmentX(Component.CENTER_ALIGNMENT);
	pane.add(banner);

	// Welcome!
	JLabel welcome = new JLabel("WELCOME!");
	welcome.setAlignmentX(Component.CENTER_ALIGNMENT);
	pane.add(welcome);

	// Point totals
	
	JLabel points = new JLabel("Your current point total: ");
	points.setAlignmentX(Component.CENTER_ALIGNMENT);
	pane.add(points);

	JLabel numP = new JLabel(Integer.toString(pointVal));
	Font f = new Font("Dialog", Font.PLAIN, 40);
	numP.setFont(f);
	numP.setAlignmentX(Component.CENTER_ALIGNMENT);
	pane.add(numP);

	// play game button
	JButton game = new JButton("Play A Game");
	game.setAlignmentX(Component.CENTER_ALIGNMENT);
	game.addActionListener(this);
	pane.add(game);
    }

    /**
     * adds components to the new user pane
     * @param pane Container pane
     */
    public void addComponentsToNewUser(Container pane) {
	pane.setLayout(new FlowLayout(FlowLayout.CENTER));
	
	JLabel banner = new JLabel(new ImageIcon(getClass().getResource("/assets/images/JoggleBanner.png")));
	pane.add(banner);

	// Information
	JLabel info = new JLabel("Please enter the following information:");
	pane.add(info);

	// first name
	JLabel newfName = new JLabel("First Name");
	pane.add(newfName);
	nfName = new JTextField(15);
	nfName.setMaximumSize(nfName.getPreferredSize());
	nfName.addActionListener(this);
	pane.add(nfName);

	// last name
	JLabel newlName = new JLabel("Last Name");
	pane.add(newlName);
	nlName = new JTextField(15);
	nlName.setMaximumSize(nlName.getPreferredSize());
	nlName.addActionListener(this);
	pane.add(nlName);

	// user name
	JLabel newuName = new JLabel("Username");
	pane.add(newuName);
	nuName = new JTextField(15);
	nuName.setMaximumSize(nuName.getPreferredSize());
	nuName.addActionListener(this);
	pane.add(nuName);
	
	// password
	JLabel passwordLabel = new JLabel("Password");
	pane.add(passwordLabel);
	password = new JPasswordField(15);
	password.setMaximumSize(password.getPreferredSize());
	password.addActionListener(this);
	pane.add(password);

	// email
	JLabel nemail = new JLabel("  E-mail");
	pane.add(nemail);
	email = new JTextField(15);
	email.setMaximumSize(email.getPreferredSize());
	email.addActionListener(this);
	pane.add(email);
	
	// privacy setting
	group = new ButtonGroup();
	
	// combo box
	JRadioButton pub = new JRadioButton("Public");
	pub.addActionListener(this);
	pub.setActionCommand("0");
	pane.add(pub);
	group.add(pub);
	
	JRadioButton res = new JRadioButton("Restricted");
	res.addActionListener(this);
	res.setActionCommand("1");
	pane.add(res);
	group.add(res);
	
	JRadioButton pri = new JRadioButton("Private");
	pri.addActionListener(this);
	pri.setActionCommand("2");
	pane.add(pri);
	group.add(pri);

	// create a user
	JButton cUser = new JButton("Create User");
	cUser.addActionListener(this);
	pane.add(cUser);
    }

    /**
     * adds components to the new game pane
     * @param pane Container pane
     */
    public void addComponentsToNewGame(Container pane) {
	pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
	JPanel panePanel = new JPanel();
	panePanel.setSize(300,350);
	
	JLabel banner = new JLabel(new ImageIcon(getClass().getResource("/assets/images/JoggleBanner.png")));
	panePanel.add(banner,BorderLayout.CENTER);

	// Open Games
	JLabel openGameLab = new JLabel("To join an existing game, select one below:");
	panePanel.add(openGameLab,BorderLayout.CENTER);

	// TODO:
	// GET PLAYABLE GAMES BY THIS USER AND POPULATE LIST
	// CURRENTLY ONLY GETS FIRST GAME CREATED, NOT ALL GAMES
	String[] games = new String[server.maxClients];
	games[0] = "Open Games:";
	
	ArrayList<Game> activeGames;
	try {
		activeGames = server.getActiveGames();
	} catch (Exception e) {
		activeGames = new ArrayList<Game>();
	}
	
	if(activeGames.size() > 0) {
		for(int i = 0; i < activeGames.size(); i++) {
			games[i+1] = activeGames.get(i).getOwner().getUserName();
		}
	} else {
		games[1] = "No games available!";
	}
	
	// Game list
	openGames = new JComboBox(games);
	openGames.setSelectedIndex(0);
	openGames.addActionListener(this);
	openGames.setMaximumSize(openGames.getPreferredSize());
	panePanel.add(openGames,BorderLayout.CENTER);

	// Play Existing Game
	JButton playEGame = new JButton("Play Existing Game");
	playEGame.addActionListener(this);
	panePanel.add(playEGame,BorderLayout.CENTER);

	// New Game
	JLabel newGame = new JLabel("Or create a game after selecting the following:");
	panePanel.add(newGame,BorderLayout.CENTER);

	// number of players in game
	String[] numP = {"# Players:","2","3","4","5","6"};
	numPlayers = new JComboBox(numP);
	numPlayers.setSelectedIndex(0);
	numPlayers.addActionListener(this);
	numPlayers.setMaximumSize(numPlayers.getPreferredSize());
	panePanel.add(numPlayers,BorderLayout.CENTER);

	// board size
	String [] bSize = {"Board Size:","4 x 4","5 x 5"};
	boardSize = new JComboBox(bSize);
	boardSize.setSelectedIndex(0);
	boardSize.addActionListener(this);
	boardSize.setMaximumSize(boardSize.getPreferredSize());
	panePanel.add(boardSize,BorderLayout.CENTER);

	// New Game
	JButton playNGame = new JButton("Create a New Game");
	playNGame.addActionListener(this);
	panePanel.add(playNGame,BorderLayout.CENTER);

	pane.add(panePanel,BorderLayout.CENTER);

    }

    /**
     * adds components to the audit pane
     * @param pane Container pane
     */
    public void addComponentsToAudit(Container pane) {
	pane.setLayout(new FlowLayout());
	JLabel banner = new JLabel(new ImageIcon(getClass().getResource("/assets/images/JoggleBanner.png")));
	banner.setAlignmentX(Component.CENTER_ALIGNMENT);
	pane.add(banner);

	JLabel welAudit = new JLabel("Welcome Auditor!");
	JLabel welAudit2 = new JLabel("Please choose one of the following options:");
	pane.add(welAudit,BorderLayout.CENTER);
	pane.add(welAudit2,BorderLayout.CENTER);

	// review games
	JButton reviewGame = new JButton("Review Games");
	reviewGame.addActionListener(this);
	pane.add(reviewGame,BorderLayout.CENTER);

	// review players
	JButton reviewPlayers = new JButton("Review Players");
	reviewPlayers.addActionListener(this);
	pane.add(reviewPlayers,BorderLayout.CENTER);

	JLabel comment = new JLabel("Please interact with console.");
	pane.add(comment,BorderLayout.CENTER);
    }

    /**
     * adds components to the admin pane
     * @param pane Container pane
     */
    public void addComponentsToAdmin(Container pane) {
	pane.setLayout(new FlowLayout());
	JLabel banner = new JLabel(new ImageIcon(getClass().getResource("/assets/images/JoggleBanner.png")));
	banner.setAlignmentX(Component.CENTER_ALIGNMENT);
	pane.add(banner);

	// add / remove words from dictionary
	JLabel wordsAD = new JLabel("Type in a word to add/del from dictionary:");
	pane.add(wordsAD);

	words = new JTextField(8);
	words.setMaximumSize(words.getPreferredSize());
	words.addActionListener(this);
	pane.add(words,BorderLayout.CENTER);

	JButton addWord = new JButton("Add");
	addWord.addActionListener(this);
	pane.add(addWord, BorderLayout.CENTER);

	JButton delWord = new JButton("Delete");
	delWord.addActionListener(this);
	pane.add(delWord, BorderLayout.CENTER);
	
	// get a list of users
	ArrayList<User> allUsers = server.getAllUsers();
	ArrayList<String> bannable = new ArrayList<String>();
	for(User u : allUsers) {
		if(u instanceof Player || u instanceof Auditor) {
			bannable.add(u.getUserName());
		}
	}
	String[] listUsers = new String[bannable.size()];
	bannable.toArray(listUsers);
	if(bannable.size() == 0) { listUsers = new String[1]; listUsers[0] = "There are no users."; }
	
	// block user
	JLabel blockU = new JLabel("Please select a user to block:");
	pane.add(blockU);

	blockUser = new JComboBox(listUsers);
	blockUser.setSelectedIndex(0);
	blockUser.addActionListener(this);
	blockUser.setMaximumSize(blockUser.getPreferredSize());
	pane.add(blockUser,BorderLayout.CENTER);

	JButton block = new JButton("Block User");
	block.addActionListener(this);
	pane.add(block, BorderLayout.CENTER);
    
	// reset a user's password
	JLabel resetU = new JLabel("Please select a user's password to change:");
	pane.add(resetU);
	
	resetUser = new JComboBox(listUsers);
	resetUser.setSelectedIndex(0);
	resetUser.addActionListener(this);
	resetUser.setMaximumSize(resetUser.getPreferredSize());
	pane.add(resetUser,BorderLayout.CENTER);

	JButton reset = new JButton("Reset PW");
	reset.addActionListener(this);
	pane.add(reset, BorderLayout.CENTER);

	// request an audit of a user
	JLabel auditU = new JLabel("Please request a user to be audited:");
	pane.add(auditU);

	auditReq = new JComboBox(listUsers);
	auditReq.setSelectedIndex(0);
	auditReq.addActionListener(this);
	auditReq.setMaximumSize(auditReq.getPreferredSize());
	pane.add(auditReq,BorderLayout.CENTER);

	JButton auditR = new JButton("Audit");
	auditR.addActionListener(this);
	pane.add(auditR, BorderLayout.CENTER);
    }

    // if a button is clicked
    /**
     * @param e ActionEvent for when the submit button is pressed
     */
    public void actionPerformed(ActionEvent e) {
	String cmd = e.getActionCommand();

	if ("Log-in".equals(cmd)) {
	    System.out.println("#### LOG IN ####");
	    String un = userName.getText();
	    System.out.println(un);
	    char[] p = passwd.getPassword();
	    String up = new String(p);

	    boolean auditB = auditBox.isSelected();
	    boolean adminB = adminBox.isSelected();
	    // cannot sign in as both auditor and admin
	    if (auditB && adminB) {
		System.out.println("You cannot sign in as both an auditor and an admin at the same time!");
	    } else {
		// check password against database record
		try {
		    if(up.length() > 0 && !up.equals(server.hash(""))) {
			// Password checks
			if (adminB) {
			    administrator = (Administrator) server.getUserFromName(un,"Administrator", up);
			    if(administrator != null) {
				System.out.println("#### LOGGED IN AS ADMINISTRATOR ####");
				loginFrame.dispose();
				adminFrame.setVisible(true);
			    }
			} else if (auditB) {
			    auditor = (Auditor) server.getUserFromName(un, "Auditor", up);
			    if(auditor != null) {
				System.out.println("#### LOGGED IN AS AUDITOR ####");
				loginFrame.dispose();
				auditFrame.setVisible(true);
			    }
			} else {
			    player = (Player) server.getUserFromName(un, "Player", up);
			    if(player != null) {
				System.out.println("#### LOGGED IN AS PLAYER ####");
				pointVal = player.getPoints();
				loginFrame.dispose();
				// welcome frame must be created here b/c of point value issue
				welcomeFrame = new JFrame("Joggle Welcome!");
				welcomeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				welcomeFrame.setSize(300,350);
				addComponentsToWelcome(welcomeFrame.getContentPane());
				welcomeFrame.setVisible(true);
			    }
			}
		    } else {
		    	System.out.println("Blank passwords are not allowed.");
		    }
		    
		} catch (Exception e1) {
		    //e1.printStackTrace();
		}
	    }
	    
	} else if ("Create an Account".equals(cmd)) {
	    System.out.println("#### CREATE AN ACCOUNT ####");
	    loginFrame.dispose();
	    newUserFrame.setVisible(true);

	} else if ("Play A Game".equals(cmd)) {
	    System.out.println("#### PLAY A GAME ####");
	    welcomeFrame.dispose();
	    newGameFrame.setVisible(true);

	} else if ("Play Existing Game".equals(cmd)) {
	    System.out.println("#### PLAY AN EXISTING GAME ####");
	    String eGame = (String) openGames.getSelectedItem();
	    System.out.println(eGame);
	    if(eGame.equals("Open Games:") || eGame.equals("No games available!")) {
	    	System.out.println("You cannot join a non-existent game!  Create one of your own.");
	    } else {
	    	Game g = server.getActiveGameFromOwner(eGame);
	    	server.addGamePlayer(player, g);
	    	newGameFrame.dispose();
	    }

	} else if ("Review Games".equals(cmd)) {
	    System.out.println("Games to Review:");
	    ArrayList<Game> games = auditor.getAuthorizedGames();
	    for (int i = 0; i < games.size(); i++) {
		try {
		    auditor.reviewGame(games.get(i));
		} catch (Exception excep1) {
		    excep1.printStackTrace();
		}
	    }
	    System.out.println("Done reviewing games.");
	    
	} else if ("Review Players".equals(cmd)) {
	    System.out.println("Players to Review:");
	    ArrayList<Player> players = auditor.getAuthorizedPlayers();
	    for (int i = 0; i < players.size(); i++) {
		try {
		    auditor.reviewPlayer(players.get(i));
		} catch (Exception excep2) {
		    excep2.printStackTrace();
		}
	    }
	    System.out.println("Done reviewing players.");

	} else if ("Create a New Game".equals(cmd)) {
	    System.out.println("#### CREATE A NEW GAME ####");
	    String nPlayers = (String)numPlayers.getSelectedItem();
	    String bSize = (String)boardSize.getSelectedItem();

	    if (nPlayers.equals("# Players:") || bSize.equals("Board Size:")) {
	    	System.out.println("please select a vlid number of players and board size.");
	    } else {
		    try {
		    	// minPlay, Integer maxPlay, Integer minPoint, Integer maxPoint, Integer maxW, Integer du, Integer sd, Integer sz, ArrayList<String> p, ArrayList<String> allowed, ArrayList<String> banned, Dictionary di, Player o, Boolean newG)
		    	Integer maxPlayers;
		    	if(numPlayers.getSelectedItem().toString().equals("# Players:")) { maxPlayers = 10; }
		    	else { maxPlayers = new Integer(numPlayers.getSelectedItem().toString()); }
		    	Integer minPoints = 0;
		    	Integer maxPoints = Integer.MAX_VALUE;
		    	Integer maxWait = 10000;
		    	Integer duration = 60000;
		    	Integer seed = 0;
		    	Integer size;
		    	String s = bSize.substring(0,1);
		    	if(s.equals("B")) { size = 4; }
		    	else { size = new Integer(s); }
             		    	if(size > 4) { 
		    		int playerPoints = server.getPlayerPoints(player);
		    		if(playerPoints < server.getBoardUnlock()) {
		    			size = 4;
		    		}
		    	}
		    	ArrayList<String> players = new ArrayList<String>();
				ArrayList<String> whiteList = new ArrayList<String>();
				ArrayList<String> banList = new ArrayList<String>();
				Dictionary d = server.getAvailableDictionaries().get(0);
		    	Game g = new Game(new Integer(2), maxPlayers, minPoints, maxPoints, maxWait, duration, seed, size, players, whiteList, banList, d, player, true);
		    	ArrayList<Game> active = server.getActiveGames();
				for(Game game : active) {
					System.out.println(game);
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
	    }
	} else if ("Add".equals(cmd)) {
	    String add = words.getText();
	    ArrayList<String> toAdd = new ArrayList<String>();
	    toAdd.add(add);
	    server.getAvailableDictionaries().get(0).addWords(toAdd);
	    System.out.println(add+" added to dictionary");
	    
	} else if ("Delete".equals(cmd)) {
	    String del = words.getText();
	    ArrayList<String> toDel = new ArrayList<String>();
	    toDel.add(del);
	    server.getAvailableDictionaries().get(0).delWords(toDel);
	    System.out.println(del+" deleted from dictionary");

	} else if ("Reset PW".equals(cmd)) {
	    String userName = (String)resetUser.getSelectedItem();
	    User user = server.getUserFromName(userName, "Player", administrator); // null
	    String newPass = "password";
	    server.setUserPassword(user, administrator, server.hash(newPass));
	    if(server.validPassword(user, newPass)) {
	    	System.out.println(user+"'s password has been reset");
	    } else {
	    	System.out.println("Unable to reset " + user+ "'s password");
	    }

	} else if ("Audit".equals(cmd)) {
	    String userName = (String)auditReq.getSelectedItem();
	    User user = server.getUserFromName(userName, "Player", administrator); // null
	    // HOW TO REQUEST AUDIT?
	    System.out.println("an audit has been requested for "+user);

	} else if ("Block User".equals(cmd)) {
	    String userName = (String)blockUser.getSelectedItem();
	    User user = server.getUserFromName(userName, "Player", administrator); // null
	    String newPass = "";
	    server.setUserPassword(user, administrator, server.hash(newPass));
	    if(server.validPassword(user, newPass)) {
	    	System.out.println(user+" has been blocked");
	    } else {
	    	System.out.println("Unable to block " + user);
	    }

	} else if ("Create User".equals(cmd)) {
	    System.out.println("#### CREATE A NEW USER ####");
	    String nfn = nfName.getText();
	    String nln = nlName.getText();
	    String nun = nuName.getText();
	    String np = password.getText();
	    String ne = email.getText();
	    String priv = group.getSelection().getActionCommand();
	    System.out.println(nfn);
	    System.out.println(nln);
	    System.out.println(nun);
	    System.out.println(ne);
	    System.out.println(priv);

	    newUserFrame.dispose();
	    try {
			Player newP = new Player(nfn, nln, nun, ne, np, 0, server, true);
		} catch (Exception e1) {
			e1.printStackTrace();
			System.out.println("Unable to create account!");
		}
	    loginFrame.setVisible(true);
	}
    }
}
